<?php 

namespace IMT\ServicesBundle\Controller;

use JMS\Serializer\SerializationContext;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use FOS\RestBundle\View\View;
use FOS\Rest\Util\Codes;

class MapsRestController extends Controller
{
	/**
	 * GET /event/{eventId}
	 *
	 * @param string $eventId
	 * @return Response
	 */
	public function getMapsAction($eventId)
	{			
		if(intval($eventId) == 0)
	    	throw new \InvalidArgumentException(400, 'L\'id est incorrect : '.$eventId);
	    
    	// Récupération de l'event
    	$repository = $this->getDoctrine()->getRepository('IMTEventBundle:Meeting');
   		$event = $repository->findOneById($eventId);
   		
   		if (!$event)
   			throw new NotFoundHttpException('L\'event ayant l\'id '.$eventId.' n\'existe pas');
   		
   		
   		// Vérification des droits d'acces
   		$securityContext = $this->get('security.context');   		
   			   		
		$view = View::create()
			->setStatusCode(200)
			->setData(array('address' => ($event->getAddress()) ?  $event->getAddress() : ''));

		return $this->get('fos_rest.view_handler')->handle($view);
	}
	
	/**
	 * Creates a new Thread from the submitted data.
	 *
	 * @param Request $request The current request
	 *
	 * @return View
	 */
	public function postMapsAction(Request $request)
	{
		$idAddresse = 0;
	
		$em = $this->getDoctrine()->getEntityManager();
		$viewHandler = $this->get('fos_rest.view_handler');
			
		if('POST' == $request->getMethod()){
			$idEvent = $request->request->get('event_id');
			
			$validator = $this->get('validator');
			
			$event = null;
						
			if($idEvent == 0){
				throw $this->createNotFoundException('L\'event "'.$idEvent.'" n\'existe pas');
			} else {
				$event = $em->getRepository('IMTEventBundle:Meeting')->find($idEvent);
					
				if(!$event)
					throw $this->createNotFoundException('L\'event "'.$idEvent.'" n\'existe pas');
			}
			
			$event->setAddresse($request->request->get('address'));
						
			//return $viewHandler->handle($this->createErrorViewResponse($event));
			$errors = $validator->validate($event);
			
			if(count($errors) > 0){
				$return['errors'] = $errors;
			
				return $viewHandler->handle($this->createErrorViewResponse($return));
			} else {
				$em = $this->getDoctrine()->getEntityManager();
				$em->persist($event);
				$em->flush();
					
				$return['idEvent'] = $event->getId();
			
				return $viewHandler->handle($this->createSuccessViewResponse($return));
			}
		} else {
			throw new MethodNotAllowedException('POST');
		}
	
	}
	
	public function deleteMapsAction($id)
	{
		$em = $this->getDoctrine()->getEntityManager();
		$viewHandler = $this->get('fos_rest.view_handler');
			
		$event = $em->getRepository('IMTEventBundle:Meeting')->find($id);
		
		$user = $this->get('security.context')->getToken()->getUser();
		
		if (!$event) {
			throw $this->createNotFoundException('L\'event "'.$id.'" n\'existe pas');
		}
		
		$event->setAddresse('');
		
		$em->persist($event);
		$em->flush();
		
		return $viewHandler->handle($this->createSuccessViewResponse(array('idEvent' => $id)));
	}
	
	private function createSuccessViewResponse($return)
	{
		$view = View::create()
		->setStatusCode(200)
		->setData($return);
	
		return $view;
	}
	
	private function createErrorViewResponse($return)
	{
		$view = View::create()
		->setStatusCode(Codes::HTTP_BAD_REQUEST)
		->setData($return);
	
		return $view;
	}
}